﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Net;

namespace Emoney.UserTraining.DAL
{
   public class SQLTools
    {
       /// <summary>
       /// 创建SqlParameter对象
       /// </summary>
       /// <param name="name">名称</param>
       /// <param name="type">类型</param>
       /// <param name="value">参数值</param>
       /// <returns>SqlParameter对象</returns>
       public static SqlParameter CreateSqlParameter(string name, SqlDbType type, object value)
       {
           if (value == null)
           {
               value = DBNull.Value;
           }
           if (!name.StartsWith("@"))
           {
               name = "@" + name;
           }
           SqlParameter sp = new SqlParameter(name, type);
           sp.Value = value;
           return sp;
       }

       public static SqlParameter CreateSqlParameter(string name, SqlDbType type)
       {
           if (!name.StartsWith("@"))
           {
               name = "@" + name;
           }
           SqlParameter sp = new SqlParameter(name, type);
           return sp;
       }

       /// <summary>
       /// 创建SqlParameter对象
       /// </summary>
       /// <param name="name">名称</param>
       /// <param name="type">类型</param>
       /// <param name="direction">参数值</param>
       /// <returns>SqlParameter对象</returns>
       public static SqlParameter CreateSqlParameter(string name, SqlDbType type, ParameterDirection direction)
       {
           if (!name.StartsWith("@"))
           {
               name = "@" + name;
           }
           SqlParameter sp = new SqlParameter(name, type);
           sp.Direction = direction;
           return sp;
       }

       /// <summary>
       /// 创建SqlParameter对象
       /// </summary>
       /// <param name="name">名称</param>
       /// <param name="type">类型</param>
       /// <param name="size">参数值</param>
       /// <param name="value">SqlParameter对象</param>
       /// <returns>SqlParameter对象</returns>
       public static SqlParameter CreateSqlParameter(string name, SqlDbType type, int size, object value)
       {
           if (value == null)
           {
               value = DBNull.Value;
           }
           if (!name.StartsWith("@"))
           {
               name = "@" + name;
           }
           SqlParameter sp = new SqlParameter(name, type, size);
           sp.Value = value;
           return sp;
       }

       /// <summary>
       /// 创建SqlParameter对象
       /// </summary>
       /// <param name="name">名称</param>
       /// <param name="type">类型</param>
       /// <param name="size">参数值</param>
       /// <param name="value">SqlParameter对象</param>
       /// <param name="direction">方向</param>
       /// <returns>SqlParameter对象</returns>
       public static SqlParameter CreateSqlParameter(string name, SqlDbType type, int size, object value, ParameterDirection direction)
       {
           if (value == null)
           {
               value = DBNull.Value;
           }
           if (!name.StartsWith("@"))
           {
               name = "@" + name;
           }
           SqlParameter sp = new SqlParameter(name, type, size);
           sp.Direction = direction;
           sp.Value = value;
           return sp;
       }

       List<SqlParameter> lstPara = new List<SqlParameter>();
       /// <summary>
       /// 添加SqlParameter对象
       /// </summary>
       /// <param name="name">名称</param>
       /// <param name="type">类型</param>
       /// <param name="value">值</param>
       public void AddSqlParameter(string name, SqlDbType type, object value)
       {
           lstPara.Add(CreateSqlParameter(name, type, value));
       }

       /// <summary>
       /// 添加SqlParameter对象
       /// </summary>
       /// <param name="name">名称</param>
       /// <param name="type">类型</param>
       /// <param name="size">大小</param>
       /// <param name="value">值</param>
       public void AddSqlParameter(string name, SqlDbType type, int size, object value)
       {
           lstPara.Add(CreateSqlParameter(name, type, size, value));
       }

       /// <summary>
       /// 添加SqlParameter对象
       /// </summary>
       /// <param name="name">名称</param>
       /// <param name="type">类型</param>
       /// <param name="size">大小</param>
       /// <param name="value">值</param>
       /// <param name="direction">方向</param>
       public void AddSqlParameter(string name, SqlDbType type, int size, object value, ParameterDirection direction)
       {
           lstPara.Add(CreateSqlParameter(name, type, size, value, direction));
       }

       /// <summary>
       /// 添加SqlParameter对象
       /// </summary>
       /// <param name="name">名称</param>
       /// <param name="type">类型</param>
       internal void AddSqlParameter(string name, SqlDbType type)
       {
           lstPara.Add(CreateSqlParameter(name, type));
       }

       /// <summary>
       /// 添加SqlParameter对象
       /// </summary>
       /// <param name="name">名称</param>
       /// <param name="type">类型</param>
       /// <param name="direction">方向</param>
       public void AddSqlParameter(string name, SqlDbType type, ParameterDirection direction)
       {
           lstPara.Add(CreateSqlParameter(name, type, direction));
       }

       /// <summary>
       /// 得到SqlParameter数组
       /// </summary>
       /// <returns>SqlParameter数组</returns>
       public SqlParameter[] GetSqlParameters()
       {
           return lstPara.ToArray();
       }

       public static DateTime? GetDateTime(object o)
       {
           if (o == DBNull.Value)
           {
               return null;
           }
           else
           {
               return Convert.ToDateTime(o);
           }
       }

       public static string GetString(object o)
       {
           if (o == null)
           {
               return null;
           }
           else if (o == DBNull.Value)
           {
               return null;
           }
           else
           {
               return o.ToString();
           }
       }

       public static Guid? GetGuid(object o)
       {
           if (o == DBNull.Value)
           {
               return null;
           }
           else
           {
               return (Guid)o;
           }
       }

       public static bool? GetBool(object o)
       {
           if (o == DBNull.Value)
           {
               return null;
           }

           if (o is bool)
           {
               return (bool)o;
           }

           try
           {
               int i = int.Parse(o.ToString());
               if (i < 1)
               {
                   return false;
               }
               else
               {
                   return true;
               }
           }
           catch
           {
               return false;
           }
       }

       public static long? GetLong(object o)
       {
           if (o == DBNull.Value)
           {
               return null;
           }
           else
           {
               return (long)o;
           }
       }

       public static int? GetInt(object o)
       {
           if (o is bool)
           {
               if ((bool)o)
               {
                   return 1;
               }
               else
               {
                   return 0;
               }
           }

           if (o == DBNull.Value)
           {
               return null;
           }
           else
           {
               return int.Parse(o.ToString());
           }
       }

       public static double? GetDouble(object o)
       {
           if (o == DBNull.Value)
           {
               return null;
           }
           else
           {
               return double.Parse(o.ToString());
           }
       }

       public static decimal? GetDecimal(object o)
       {
           if (o == DBNull.Value)
           {
               return null;
           }
           else
           {
               try
               {
                   return Convert.ToDecimal(o);
               }
               catch
               {
                   return null;
               }
           }
       }

       public static int GetDBLenth(string str)
       {
           return System.Text.Encoding.GetEncoding("gb2312").GetByteCount(str);
       }

       public SqlParameter GetSqlParameterByName(string name)
       {
           if (!name.StartsWith("@"))
           {
               name = "@" + name;
           }
           foreach (SqlParameter sp in lstPara)
           {
               if (sp.ParameterName == name)
               {
                   return sp;
               }
           }
           return null;
       }

       public static string LoadData(string url)
       {
           string rel = "";
           try
           {
               StringBuilder sb = new StringBuilder();
               WebClient wc = new WebClient();
               wc.Encoding = System.Text.Encoding.Default;
               rel = wc.DownloadString(new Uri(url));
           }
           catch (Exception ex) { }
           return rel;
       }

       public static DateTime ToDateTime(object obj, DateTime v)
       {
           DateTime rel = v;
           try
           {
               rel = DateTime.Parse(obj.ToString());
           }
           catch { }
           return rel;
       }
   }
}
